<?php

class NoticeAction extends Action {

    function index() {
        $notice = M("notice");
        import('ORG.Util.Page'); // 导入分页类
        $count = $notice->where("")->order("sort asc,ftime desc")->count(); // 查询满足要求的总记录数
        $Page = new Page($count, 5);
        $show = $Page->show(); // 分页显示输出
        $list = $notice->where("")->order("sort asc,ftime desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $show); // 赋值分页输出
        $this->assign("list", $list);
        $this->display();
    }

    //添加公告
    function add() {
        if ($_POST) {
            $notice = M("notice");
            $data['title'] = $this->_param("title");
            $data['content'] = $this->_param("content");
            $data['sort'] = $this->_param("sort");
            $data['str'] = $this->_param("str");
            $data['ftime'] = date("Y-m-d H:i:s");
            $notice->add($data);
            if ($notice) {
                $this->redirect("/Notice");
            }
        } else {
            $this->display();
        }
    }

    //编辑公告
    function edit() {
        $notice = M("notice");
        $id = $this->_param("id");
        if (!$_POST) {
            $list = $notice->where("id = $id")->select();
            $this->assign('list', $list[0]);
            $this->display();
        } else {
            $data['title'] = $this->_param("title");
            $data['content'] = $this->_param("content");
            $data['sort'] = $this->_param("sort");
            $data['str'] = $this->_param("str");
            $data['xtime'] = date("Y-m-d H:i:s");
            $notice->where("id = $id")->save($data);
            if ($notice) {
                $this->redirect("/Notice");
            }
        }
    }

    //删除公告
    function del() {
        $notice = M("notice");
        $id = $this->_param("id");
        $notice->where("id = $id")->delete();
        if ($notice) {
            $this->redirect("/Notice");
        }
    }

}

?>
